__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db
from product import Product, getProductListXml, getProductCategory, getTitleTok
import time
import logging
from xml.dom import minidom
from parser import getText

class Tok(db.Model):
	tok = db.StringProperty()
	url = db.StringProperty()
	count = db.IntegerProperty()

class SearchRecord(db.Model):
	tok = db.StringProperty()
	searchtime = db.DateTimeProperty(auto_now_add=True) 

def getTokDictFromXML(xml):
	tok_dict = {}
	if xml:
		dom = minidom.parseString(xml.content)
		#logging.info('dom paresed in updateIndex')
		words = dom.getElementsByTagName("words")[0]
		wordlist = words.getElementsByTagName("word")
		for word in wordlist:
		 	tok = getText(word.childNodes)
			if tok in tok_dict:
				tok_dict[tok] += 1
			else:
				tok_dict[tok] = 1
	return tok_dict

def updateIndex(productInsert, productDelete):
	#delete index
	for product in productDelete:
		q = db.GqlQuery("SELECT * from Tok where url = :1", product.url)
		for tok in q:
			tok.delete()
			#logging.info(tok.url + " deleted")
	
	#insert index
	for product in productInsert:
		#logging.info(product.title)
		tok_dict = getTokDictFromXML(getTitleTok(product.title.encode('utf-8'), 1))
		for key, value in tok_dict.items():
			if len(key.encode('utf-8')) > 4:
				tok = Tok()
				tok.url = product.url
				tok.tok = key.lower()
				tok.count = value
				tok.put()
			#	logging.info(tok.url + "," + tok.tok + " added")

def getSearchResult(tokList):
	emptyWord = (' ', ',', '.', ';')
	setList = []
	for tok in tokList:
		if tok not in emptyWord and tok != '':
			q = db.GqlQuery("SELECT * from Tok where tok = :1", tok.lower())
			re = q.fetch(1000)
			if len(re) > 0:
				setList.append(set(re))
			recordSearch(tok.lower())

	if len(setList) == 0:
		return []
	else:
		resultSet = setList[0]

	for i in range(1,len(setList)):
		tempSet = resultSet & setList[i]
		if len(tempSet) != 0:
			resultSet = tempSet
	
	productList = []
	for result in resultSet:
		q = db.GqlQuery("SELECT * from Product where url = :1", result.url)
		p = q.fetch(1)
		if len(p) > 0:
			productList.append(p[0])
	
	productList.sort()
	
	return productList

def recordSearch(tok):
	re = SearchRecord()
	re.tok = tok
	re.put()
